package com.gonsin.ta.logic.parse.operator;

import cn.hutool.core.util.ArrayUtil;
import com.gonsin.ta.logic.parse.Chain;
import com.gonsin.ta.logic.exceptions.TaException;
import com.gonsin.ta.logic.parse.condition.Condition;
import com.gonsin.ta.logic.parse.condition.SwitchCondition;
import com.gonsin.ta.logic.node.Node;
import com.gonsin.ta.logic.enums.NodeTypeEnum;
import com.ql.util.express.Operator;

/**
 * 运行节点
 */
public class SwitchOperator extends Operator {

    private Chain chain;

    public SwitchOperator(Chain chain) {
        this.chain = chain;
    }

    @Override
    public Object executeInner(Object[] objects) throws Exception {
        if (ArrayUtil.isEmpty(objects)){
            throw new TaException("chain id = {}, SWITCH方法必须包含一个参数", chain.getChainId());
        }

        if (objects.length != 1){
            throw new TaException("chain id = {}, SWITCH方法只能包含一个参数", chain.getChainId());
        }

        if(!(objects[0] instanceof Node)){
            throw new TaException("chain id = {}, SWITCH方法只能支持组件参数", chain.getChainId());
        }

        // 判断该组件方法是switch组件
        Node node = (Node) objects[0];
        if(node.getNodeType() != NodeTypeEnum.SWITCH){
            throw new TaException("chain id = {}, SWITCH方法只能支持SWITCH组件参数", chain.getChainId());
        }

        Condition result =  new SwitchCondition(node);
        return result;
    }
}
